home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / hardware / kbdhand9 / key16.asm < prev    next >
Encoding:
Assembly Source File  |  1994-06-15  |  8.1 KB  |  220 lines

  1. ;******************************************************************************
  2. ;* INT9 keyboard handler #9 (16-bit)
  3. ;* by Lee Hamel (hamell@cs.pdx.edu)
  4. ;* June 15th, 1994
  5. ;*
  6. ;* All keyboard buffering code by James Ketrenos (ketrenoj@cs.pdx.edu)
  7. ;******************************************************************************
  8. .model large
  9. .286
  10. JUMPS
  11.  
  12. PIC_CMD                 EQU     20h
  13. NONSPEC_EOI             EQU     20h
  14.  
  15. .code
  16. PUBLIC  _keys
  17.         _keys           db      256 dup (0)
  18. PUBLIC  _keynumpress
  19.         _keynumpress    db      0
  20. PUBLIC  _keylast
  21.         _keylast        db      0
  22. PUBLIC  _oldkeylast
  23.         _oldkeylast     db      0
  24.  
  25. toASCII                 db      0,27,'1234567890-=',14,15
  26.                         db      'qwertyuiop',0,0,13,0,'as'
  27.                         db      'dfghjkl',0,0,'''',0,0,'zxcv'
  28.                         db      'bnm',0,'./',0,0,0,' ',0,1,2,3,4,5
  29.                         db      6,7,8,9,10,0,0,24,25,26,'-',21,22,23,0,18
  30.                         db      19,20,16,17,0,0,0,11,12,0,0,0,0,0,0,0
  31.                         db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  32.                         db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  33.                         db      0,27,'!',0,'#',0,'%',0,0,0,0,0,0,0,14,15
  34.                         db      'QWERTYUIOP',0,0,13,0,'AS'
  35.                         db      'DFGHJKL:',0,'"',0,0,'ZXCV'
  36.                         db      'BNM',0,0,'?',0,0,0,' ',0,1,2,3,4,5
  37.                         db      6,7,8,9,10,0,0,24,25,26,'-',21,22,23,0,18
  38.                         db      19,20,16,17,0,0,0,11,12,0,0,0,0,0,0,0
  39.                         db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  40.                         db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  41. e0flag                  db      0
  42. label oldint9 dword
  43. oldint9off              dw      ?
  44. oldint9seg              dw      ?
  45. keyinst                 db      0
  46. keyhead                 db      0
  47. keytail                 db      0
  48. keybuffer               db      256 dup(0)
  49. keyrepeat               db      256 dup(0)
  50.  
  51. ;*****************************************************************************
  52. ;
  53. ; ClearKeys and GetKey routines by James Ketrenos (ketrenoj@cs.pdx.edu)
  54. ;
  55. ;*****************************************************************************
  56.  
  57.                         PUBLIC  _ClearKeys
  58. _ClearKeys              PROC    FAR
  59.                 push    bx
  60.         ;; *********************************************
  61.         ;; Clear the buffer by setting TAIL on HEAD
  62.         ;; *********************************************
  63.                 mov     bl,keyhead
  64.                 mov     keytail,bl
  65.                 mov     [_keylast],0
  66.                 pop     bx
  67.                 ret
  68. _ClearKeys              ENDP
  69.  
  70.                         PUBLIC  _GetKey
  71. _GetKey                 PROC    FAR
  72.                 push    ax bx
  73.                 xor     ax,ax
  74.                 xor     bx,bx
  75.         ;; *********************************************
  76.         ;; Check to see if buffer is EMPTY
  77.         ;; *********************************************
  78.         mov     bl,keytail              ;; Point to the end of the buffer
  79.         cmp     bl,keyhead              ;; : and if it is equal to the front,
  80.         jz      GKDone                  ;; : then the buffer is empty, so wait.
  81.  
  82.         ;; *********************************************
  83.         ;; Get next key from KEYBOARD BUFFER
  84.         ;; *********************************************
  85.         mov     al,keybuffer[bx]        ;; Fetch the ASCII Code
  86.         inc     [keytail]               ;; Increment the KeyTail
  87.                                         ;; : NOTE that it loops at bl==256
  88.                 cmp     al,1Bh          ; Esc
  89.                 jne     GKDone
  90.                 cmp     keyrepeat[1],2  ; dont want Esc repeating
  91.                 jb      GKDone
  92.                 xor     al,al
  93. GKDone:
  94.                 mov     [_keylast],al
  95.                 pop     bx ax
  96.                 ret                     ;; Return to caller and all that stuff
  97. _GetKey                 ENDP
  98.  
  99.                         PUBLIC  _Set_New_Int9
  100. _Set_New_Int9           PROC    FAR
  101.                 cmp     [keyinst],1
  102.                 je      exitnew9
  103.  
  104.                 mov     [keyinst],1
  105.                 cli
  106.                 pusha
  107.                 push    ds es
  108.                 mov     ax,3509h
  109.                 int     21h
  110.                 mov     [oldint9off],bx
  111.                 mov     [oldint9seg],es
  112.                 mov     ax,2509h
  113.                 mov     dx,offset New_Int9
  114.                 push    cs
  115.                 pop     ds
  116.                 int     21h
  117.                 pop     es ds
  118.                 popa
  119.                 sti
  120. exitnew9:       ret
  121. _Set_New_Int9           ENDP
  122.  
  123.                         PUBLIC  _Set_Old_Int9
  124. _Set_Old_Int9           PROC    FAR
  125.                 cmp     [keyinst],0
  126.                 je      exitold9
  127.  
  128.                 mov     [keyinst],0
  129.                 cli
  130.                 pusha
  131.                 push    ds
  132.                 mov     dx,[oldint9off]
  133.                 mov     ds,[oldint9seg]
  134.                 mov     ax,2509h
  135.                 int     21h
  136.                 pop     ds
  137.                 popa
  138.                 sti
  139. exitold9:       ret
  140. _Set_Old_Int9           ENDP
  141.  
  142. New_Int9                PROC    FAR
  143.                 push    ax bx cx ds
  144.                 mov     ax,@data
  145.                 mov     ds,ax
  146.  
  147.                 in      al,60h
  148.                 mov     ah,al
  149.  
  150. ; these 5 lines of code only necessary on XT's
  151. ;                in      al,61h
  152. ;                or      al,80h
  153. ;                out     61h,al
  154. ;                and     al,7Fh
  155. ;                out     61h,al
  156.  
  157. ;                pushf                          ; this calls the BIOS handler
  158. ;                call    [oldint9]
  159.  
  160.                 cmp     ah,0E0h
  161.                 jae     e0flagset
  162.  
  163.                 xor     bx,bx
  164.                 mov     bl,ah
  165.                 and     bl,01111111b
  166.                 add     bl,[e0flag]
  167.                 mov     [e0flag],0
  168.  
  169.                 rol     ah,1
  170.                 jc      keyrelease
  171.  
  172. keypress:       mov     ah,1
  173.                 sub     ah,_keys[bx]            ; old key status
  174.                 add     [_keynumpress],ah
  175.                 mov     _keys[bx],1             ; key pressed
  176.                 mov     bh,_keys[2ah]           ; get left shift status
  177.                 or      bh,_keys[36h]           ; get right shift status
  178.                 ror     bh,1                    ; put in bit 7
  179.                 add     bl,bh                   ; final key value
  180.                 xor     bh,bh                   ; clear for index use
  181.                 mov     al,toASCII[bx]          ; get translated value
  182.  
  183.         ;; *********************************************
  184.     ;; Check to see if buffer is FULL
  185.     ;; *********************************************
  186.         mov     cx,bx
  187.         xor     bx,bx
  188.         mov     bl,keyhead              ;; Point to the front of the buffer
  189.         inc     bl                      ;; : and if the next space places
  190.         cmp     bl,keytail              ;; : us on the end of the buffer,
  191.         jz      int9_done               ;; : then the buffer is full ...
  192.         dec     bl                      ;; : otherwise, we help fill it.
  193.  
  194.     ;; *********************************************
  195.     ;; Put key into KEYBOARD BUFFER (Adding scancode)
  196.     ;; *********************************************
  197.         mov     keybuffer[bx],al        ;; Save the ASCII Code into buffer
  198.         inc     bl                      ;; Increment the KeyHead
  199.         mov     keyhead,bl              ;; : NOTE that it loops at bl==256
  200.         mov     bx,cx
  201.         cmp     keyrepeat[bx],2
  202.         je      int9_done
  203.         inc     keyrepeat[bx]
  204.                 jmp     int9_done
  205.  
  206. keyrelease:
  207.                 dec     [_keynumpress]
  208.                 mov     _keys[bx],0             ; key released
  209.                 mov     keyrepeat[bx],0
  210.                 jmp     int9_done
  211.  
  212. e0flagset:      mov     [e0flag],128
  213. int9_done:      mov     al,NONSPEC_EOI
  214.                 out     PIC_CMD,al
  215.                 pop     ds cx bx ax
  216.                 iret
  217. New_Int9                ENDP
  218.  
  219.                 END
  220.